<html>
<head><meta charset="utf-8"><title>Accessing the compilation target from a procedural macro · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20the.20compilation.20target.20from.20a.20procedural.20macro.html">Accessing the compilation target from a procedural macro</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="224098338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20the%20compilation%20target%20from%20a%20procedural%20macro/near/224098338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20the.20compilation.20target.20from.20a.20procedural.20macro.html#224098338">(Jan 26 2021 at 20:30)</a>:</h4>
<p>Procedural macros have access to environment variables like <code>CARGO_PKG_VERSION</code> (which is the version of the <em>calling</em> crate), but for some reason <code>TARGET</code> is not available. Is there any other way to access the compilation target of the calling crate? Why is <code>TARGET</code> unset, but other variables are available?</p>



<a name="224213035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20the%20compilation%20target%20from%20a%20procedural%20macro/near/224213035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20the.20compilation.20target.20from.20a.20procedural.20macro.html#224213035">(Jan 27 2021 at 17:06)</a>:</h4>
<blockquote>
<p>Why is <code>TARGET</code> unset, but other variables are available?</p>
</blockquote>
<p><code>TARGET</code> is only <a href="https://doc.rust-lang.org/nightly/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts">set for build scripts</a>. It's not <a href="https://doc.rust-lang.org/nightly/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates">set for crates</a>.</p>
<blockquote>
<p>Is there any other way to access the compilation target of the calling crate?</p>
</blockquote>
<p>I don't know. I haven't found a way yet, but <span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span> for this feature. I wish procedural macros can access the compilation target, too.</p>



<a name="224213126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20the%20compilation%20target%20from%20a%20procedural%20macro/near/224213126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20the.20compilation.20target.20from.20a.20procedural.20macro.html#224213126">(Jan 27 2021 at 17:07)</a>:</h4>
<p>ah, that explains it, thanks!</p>



<a name="224213174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20the%20compilation%20target%20from%20a%20procedural%20macro/near/224213174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20the.20compilation.20target.20from.20a.20procedural.20macro.html#224213174">(Jan 27 2021 at 17:07)</a>:</h4>
<p>I was able to easily work around this by basically moving the target-dependent logic to a downstream crate</p>



<a name="224213197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20the%20compilation%20target%20from%20a%20procedural%20macro/near/224213197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20the.20compilation.20target.20from.20a.20procedural.20macro.html#224213197">(Jan 27 2021 at 17:07)</a>:</h4>
<p>But that might not always work</p>



<a name="224599036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20the%20compilation%20target%20from%20a%20procedural%20macro/near/224599036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20the.20compilation.20target.20from.20a.20procedural.20macro.html#224599036">(Jan 30 2021 at 18:50)</a>:</h4>
<p>We should probably expose the target somehow, to give the proc macro equivalent capability to code that checks cfg.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>